package sharping.appilog.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import sharping.appilog.Detail_Write;
import sharping.appilog.R;
import sharping.appilog.app;
import sharping.appilog.DataTree.SearchBiz_Data;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;

public class Fragment_Write extends Fragment implements OnItemClickListener {

	private static final String url = "http://www.sharping.co.kr/rmobile/select_biz_location.php";

	ArrayList<SearchBiz_Data> list_data;
	LayoutInflater inflater;
	ListView list;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_write, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		inflater = LayoutInflater.from(getActivity());
		list = (ListView) getActivity().findViewById(R.id.fragment_write_list);
		list.setOnItemClickListener(this);
		app.mQueue.add(request);
	}

	private Listener<String> success = new Listener<String>() {
		public void onResponse(String request) {
			try {
				JSONArray array = new JSONArray(request);
				list_data = new ArrayList<SearchBiz_Data>();
				for (int i = 0; i < array.length(); i++) {

					list_data.add(new SearchBiz_Data(array.getJSONObject(i)
							.getString("CUserId"), array.getJSONObject(i)
							.getString("CBizName"), array.getJSONObject(i)
							.getString("CBizLat"), array.getJSONObject(i)
							.getString("CBizLng"), array.getJSONObject(i)
							.getString("CBizAdd"), array.getJSONObject(i)
							.getString("CBizCategory"), array.getJSONObject(i)
							.getString("CBizALLCategory")));

				}

				list.setAdapter(new mAdapter());

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		};
	};

	private ErrorListener fail = new ErrorListener() {
		public void onErrorResponse(com.android.volley.VolleyError err) {

		};
	};

	private StringRequest request = new StringRequest(Method.POST, url,
			success, fail) {
		protected java.util.Map<String, String> getParams()
				throws com.android.volley.AuthFailureError {
			Map<String, String> params = new HashMap<String, String>();

			params.put("UserId", app.UserId);
			params.put("BizLat", "35.0");
			params.put("BizLng", "129.0");

			return params;
		};
	};

	private class mAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list_data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list_data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			Holder v;

			if (convertView == null) {
				v = new Holder();
				convertView = inflater.inflate(R.layout.list_bizsearch, null);
				v.name = (TextView) convertView
						.findViewById(R.id.list_bizsearch_name);
				v.lat = (TextView) convertView
						.findViewById(R.id.list_bizsearch_lat);

				convertView.setTag(v);
			} else {
				v = (Holder) convertView.getTag();
			}

			if (!(list_data.get(position).CBizLat.equals("null"))) {
				float[] distance = new float[2];
				Location.distanceBetween(35, 129,
						Double.valueOf(list_data.get(position).CBizLat),
						Double.valueOf(list_data.get(position).CBizLng),
						distance);
				v.lat.setText(OffsetDistance(distance[0]));

			} else {
				v.lat.setText("");
			}
			v.name.setText(list_data.get(position).CBizName);

			return convertView;
		}

		private class Holder {
			TextView name, lat;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position,
			long id) {
		Intent intent = new Intent(getActivity(), Detail_Write.class);
		intent.putExtra("CBizId", list_data.get(position).CUserId);
		intent.putExtra("CBizName", list_data.get(position).CBizName);
		intent.putExtra("CTimeLineLat", list_data.get(position).CBizLat);
		intent.putExtra("CTimeLineLng", list_data.get(position).CBizLng);
		intent.putExtra("CBizAdd", list_data.get(position).CBizAdd);
		intent.putExtra("CBizCategory", list_data.get(position).CBizCategory);
		intent.putExtra("CBizALLCategory",
				list_data.get(position).CBizALLCategory);
		getActivity().startActivity(intent);
	}

	public String OffsetDistance(double distance) {
		int km = (int) (distance / 1000);
		int m = (int) distance;

		if (distance > 1000) {
			return String.valueOf(km) + " km";
		} else {
			return String.valueOf(m) + " m";
		}
	}
}
